package com.tanhua.dubbodb.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.model.domain.BlackList;
import com.tanhua.model.domain.Settings;
import com.tanhua.dubbodb.mapper.BlackListMapper;
import com.tanhua.dubbodb.mapper.SettingsMapper;
import com.tanhua.dubbointerface.api.SettingsApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SettingsApiImpl implements SettingsApi {

    @Autowired
    private SettingsMapper settingsMapper;
    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 通过userId查询通用设置信息
     * @param userId
     * @return
     */
    @Override
    public Settings findSettingsByUserId(Long userId) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return settingsMapper.selectOne(queryWrapper);
    }

    /**
     * 当查询数据库中没有记录的时候，调用保存方法将新的设置保存
     * @param settings
     * @return
     */
    @Override
    public Long save(Settings settings) {
        settingsMapper.insert(settings);
        return settings.getId();
    }

    /**
     * 更新通用设置
     * @param settings
     */
    @Override
    public void update(Settings settings) {
        settingsMapper.updateById(settings);
    }


    /**
     * 从黑名单中删除黑名单用户信息
     */
    @Override
    public void deleteBlacklist(BlackList blackList) {
        blackListMapper.deleteById(blackList.getId());
    }
}
